package com.ahora.org.controller.biometrico.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ahora.org.ejb.dao.util.ConexionBD;

/**
 * Servlet implementation class ServletRevisarDeclaracionJurada
 */
@WebServlet("/ServletRevisarDeclaracionJurada")
public class ServletRevisarDeclaracionJurada extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int idPersona = Integer.parseInt(request.getParameter("idPersona"));
		Blob image = null;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		ServletOutputStream out = response.getOutputStream();
		try {
			con = ConexionBD.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery("select declaracion from tb_persona where id_persona="+idPersona+"");
			if (rs.next()) {
				image = rs.getBlob(1);
			} else {
				response.setContentType("text/html");
				out.println("<html><head><title>Error del Sistema</title></head>");
				out.println("<body><h4><font color='red'>No se encontro el pdf especifico</font></h4></body></html>");
				return;
			}
			response.setContentType("application/pdf");
			InputStream in = image.getBinaryStream();
			int length = (int) image.length();
			int bufferSize = 1024;
			byte[] buffer = new byte[bufferSize];
			while ((length = in.read(buffer)) != -1) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.flush();
		} catch (Exception e) {
			response.setContentType("text/html");
			out.println("<html><head><title>Deshabilidado mostrar el PDF</title></head>");
			out.println("<body><h4><font color='red'>Error al mostrar la imagen=" + e.getMessage() + "</font></h4></body></html>");
			return;
		} finally {
			try {
				rs.close();
				stmt.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
